package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.personservice;
import com.dk.service.rolerservice;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("person")
public class personcontroller {
    @Reference
    private personservice personservice;
    @Reference
    private rolerservice rolerservice;
    @RequestMapping("toshow")
    public String toshow(){
        return "person/show";
    }
    @RequestMapping("getshow")
    @ResponseBody
    public List<TPerson> getshow(){
        List<TPerson> list = personservice.selectgetinfo();
        return list;
    }

    @RequestMapping("getinfo")
    @ResponseBody
    public TPerson getinfo(TPerson tPerson, HttpServletRequest request){
        TPerson i = personservice.selectInfo(tPerson);
        HttpSession session = request.getSession();
        session.setAttribute("userLogin",i);
        return i;
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<TRole> list = rolerservice.selecteGetInfo();
        model.addAttribute("list",list);
        return "person/insert";
    }
    @RequestMapping("insertUser")
    @ResponseBody
    public boolean insertUser(TPerson tPerson, @RequestParam("roleId")Integer[] roleId){

        return personservice.insertnraandactor(tPerson,roleId);
    }


    @RequestMapping("toupdate/{pid}")
    public String toupdate(@PathVariable("pid")Integer pid, Model model){
        TPerson tPerson = personservice.selectbynid(pid);
        model.addAttribute("n",tPerson);
        List<TRole> actors = rolerservice.selecteGetInfo();
        model.addAttribute("list",actors);
        List<Integer> aids = rolerservice.selectnraandactor(pid);
        model.addAttribute("aids",aids);
        return "person/update";
    }
    @RequestMapping("updateNra")
    @ResponseBody
    public boolean updateNra(TPerson tPerson, @RequestParam("roleId")Integer[] roleId){
        return personservice.updateNra(tPerson,roleId);
    }

    @RequestMapping("deleteUser/{pid}")
    @ResponseBody
    public boolean deleteUser(@PathVariable("pid")Integer pid){
        return personservice.deleteBynid(pid);
    }
}
